package MyStack;

import java.util.ArrayDeque;
import java.util.Deque;

//最小栈  https://leetcode.cn/problems/min-stack/description/
class MinStack {
    Deque<Integer> stack1;
    Deque<Integer> stackM;

    public MinStack() {
        stack1 = new ArrayDeque<>();
        stackM = new ArrayDeque<>();
    }
    
    public void push(int val) {
        if (isEmpty()) {
            stack1.push(val);
            stackM.push(val);
        }else {
            int val1 = stackM.peek();
            //相等的最小值也要放入最小栈
            if (val1 >= val) {
                stackM.push(val);
            }
            stack1.push(val);
        }
    }
    
    public void pop() {
        if (isEmpty()){
            return;
        }
        int val = stack1.pop();
        if(val == stackM.peek()) {
            stackM.pop();
        }
    }
    
    public int top() {
        if (isEmpty()){
            return -1;
        }
        return stack1.peek();
    }
    
    public int getMin() {
        if (isEmpty()) {
            return -1;
        }
        return stackM.peek();
    }

    public boolean isEmpty() {
        return stack1.isEmpty() && stackM.isEmpty();
    }
}